//
//  Controller21.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/16.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

//SwipeCell

class Controller21: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var tableView : UITableView!
    
    var data = [
        Pattern(image: "1", name: "Pattern Building"),
        Pattern(image: "2", name: "Joe Beez"),
        Pattern(image: "3", name: "Car It's car"),
        Pattern(image: "4", name: "Floral Kaleidoscopic"),
        Pattern(image: "5", name: "Sprinkle Pattern"),
        Pattern(image: "6", name: "Palitos de queso"),
        Pattern(image: "7", name: "Ready to Go? Pattern"),
        Pattern(image: "8", name: "Sets Seamless"),
        ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI();
    }

    func configUI(){
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.addSubview(tableView)
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.register(UINib(nibName: "SwapCell", bundle: nil), forCellReuseIdentifier: "aaa")
        tableView.tableFooterView = UIView()
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "aaa", for: indexPath) as! SwapCell
        let pattern = data[indexPath.row]
        cell.pattern = pattern
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .default, title: "🗑\n删除") { (action, indexPath) in
            print("delete action \(indexPath)")
        }
        deleteAction.backgroundColor = .red;
        
        let shareAction = UITableViewRowAction(style: .default, title: "🌟\n分享到微信") { (action, indexPath) in
            print("share action \(indexPath)")
            let pattern = self.data[indexPath.row]
            let item = UIImage(named: "t\(pattern.image)")!
            let activity = UIActivityViewController(activityItems: [item], applicationActivities: nil)
            self.present(activity, animated: true, completion: nil);
        }
        shareAction.backgroundColor = .blue;

        let downloadAction = UITableViewRowAction(style: .default, title: "￬\n下载") { (action, indexPath) in
            print("down action \(indexPath)")
        }
        downloadAction.backgroundColor = .gray;

        
        return [deleteAction,shareAction,downloadAction];
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    
}
